//
//  ZRScanView.m
//  简书:https://www.jianshu.com/u/043e94ca450f
//
//  Created by 黄涛 on 2018/8/23.
//  Copyright © 2018年 黄涛(ZR:zero). All rights reserved.
//  扫一扫 UI
#import "ZRScanView.h"
#import "ZRScanAreaView.h"
@interface ZRScanView ()

/** 网格Layer */
@property (nonatomic , weak) CALayer * gridsLayer;
/** 蒙版 */
@property (nonatomic , weak) UIView * maskView;
/** 扫码区域 */
@property (nonatomic , weak) ZRScanAreaView * scanAreaView;


@end
@implementation ZRScanView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        self.backgroundColor = [UIColor clearColor];
    
        [self maskView];
        [self scanAreaView];
    }
    return self;
}



- (void)layoutSubviews{
    [super layoutSubviews];
    
    
    CGFloat rectX = 60;
    CGFloat rectW = self.bounds.size.width - rectX * 2 - 2;
    CGFloat rectY = (self.bounds.size.height - rectW) / 3;
    
    [self.maskView zr_setMaskLayerWithRect:CGRectMake(rectX, rectY, rectW , rectW)];
    
    self.maskView.frame = self.bounds;
    self.scanAreaView.frame = CGRectMake(rectX, rectY, rectW , rectW);
    self.scanAreaView.backgroundColor = [UIColor clearColor];
    [self gridsLayer];
    [self startAnimation];
}



#pragma mark - ----------------------- 其他方法 -----------------------
#pragma mark - 扫描动画
/** 开始 扫描动画 */
- (void)startAnimation{
    
    CGFloat rectW = self.bounds.size.width - 60 * 2;
    
    CABasicAnimation * animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.fromValue = @(0);
    animation.toValue = @(rectW);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 1.75;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [_gridsLayer addAnimation:animation forKey:@"position.y"];
    
}

/** 暂停 扫描动画 */
- (void)stopAnimation{
    
    [_gridsLayer removeAnimationForKey:@"bounds.size.height"];
}


#pragma mark - ---------------------- set & get ----------------------
/** 网格 */
- (CALayer *)gridsLayer{
    if (!_gridsLayer) {
        
        CGFloat rectW = self.bounds.size.width - 60 * 2;
        CGFloat rectY = (self.bounds.size.height - rectW) / 3;
        CGFloat gap = ( rectW ) / 50;
        CGFloat lineW = 0;
        
        CALayer * layer = [CALayer layer];
        
//        layer.frame = CGRectMake(60 - rectW / 2, rectY - rectW / 2, rectW, rectW);
        layer.frame = CGRectMake(0, 0, rectW, rectW);
        
        layer.borderColor = [UIColor greenColor].CGColor;
        layer.borderWidth = 1;
        layer.masksToBounds = YES;
        layer.anchorPoint = CGPointMake(0.5,1);
//        [self.scanAreaLayer addSublayer:layer];
        
        int y = 50;
        for (int i = 0; i < 50 * 50; i++) {

            lineW = y > 48 ? 1 : 0.5;

            CALayer * aLayer = [CALayer layer];
            aLayer.frame = CGRectMake(i % 50 * gap, y * gap, gap, gap);
            aLayer.borderColor = [[UIColor greenColor] colorWithAlphaComponent:y * 0.02] .CGColor;
            aLayer.borderWidth = lineW;
            [layer addSublayer:aLayer];

            y = i % 50 == 0 ? y - 1 : y;

        }
        
        _gridsLayer = layer;
    }
    
    return _gridsLayer;
    
}

/** 蒙版 */
- (UIView *)maskView{
    if (!_maskView) {
        
        UIView * maskView = [[UIView alloc] init];
        maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self addSubview:maskView];
        _maskView = maskView;
    }
    return _maskView;
}

/** 扫描区域 */
- (ZRScanAreaView *)scanAreaView{
    if (!_scanAreaView) {
        
        ZRScanAreaView * view = [[ZRScanAreaView alloc] init];
        view.userInteractionEnabled = YES;
        [self addSubview:view];
        _scanAreaView = view;
    }
    return _scanAreaView;
}


@end
